﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.CrmContacto.Code;
using CrmContacto.Contracts;
using CrmContacto.Contracts.Contracts.Search;
using Web.Framework;
using CrmContacto.WebApp.Code.InnerClasses;
using Web.Framework.Translate;
using CrmContacto.Contracts.Contracts;
using Web.Framework.Mapper;
using Web.CrmContacto.Code.Security;
namespace CrmContacto.WebApp.App
{
    public partial class CompanyConfigurationEdit : BasePage
    {
        public CompanyConfiguration configuration
        {
            get
            {
                return (CompanyConfiguration)ViewState["configuration"];
            }
            set
            {
                ViewState["configuration"] = value;
            }
        }

        private List<LinkButton> actionButtons = new List<LinkButton>();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            LinkButton guardarButton = new LinkButton();
            guardarButton.ID = "BoCommandGuardar";
            guardarButton.Text = LocalizationProvider.Instance["Bo.Command.Guardar"];
            guardarButton.Click += new EventHandler(lnkGuardar_Click);
            actionButtons.Add(guardarButton);

            //LinkButton volverButton = new LinkButton();
            //SetJavascriptForClientClickDirtyChech(volverButton);
            //volverButton.ID = "BoCommandVolver";
            //volverButton.Text = LocalizationProvider.Instance["Bo.Command.Volver"];
            //volverButton.Click += new EventHandler(lnkCancelar_Click);
            //actionButtons.Add(volverButton);

            base.LoadContextActions(actionButtons);
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            try
            {
                HasPermissionToSeeMe(CrmSecurity.BRAND_MANAGE);
                if (!this.IsPostBack)
                {
                    configuration = GetCompanyConfigurationList(this).Where(c => c.CompanyId == BasePage.CompanyId).Single();

                    Bind();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected override void ApplySecurity()
        {
            base.ApplySecurity();
            //this.lnkGuardar.Visible = HasPermissionFor(PlanProdSecurity.CAUSADESVIO_EDITAR);
        }

        private void Bind()
        {
            this.txtCssFilePath.Text = configuration.CssFilePath;
            this.txtLogoImgFilePath.Text = configuration.LogoImgFilePath;

            // los key value
            List<CompanyConfigurationKeyValue> configKeyValueList = GetCompanyConfigurationKeyValue(this, BasePage.CompanyId);
            foreach (CompanyConfigurationKeyValue configKeyValue in configKeyValueList)
            {
                TextBox txtBox = (TextBox)pnlMain.FindControl("txt" + configKeyValue.Key);
                if (txtBox != null)
                {
                    txtBox.Text = configKeyValue.Value;
                }
            }
        }

        private void SetData()
        {
            configuration.CssFilePath = this.txtCssFilePath.Text;
            configuration.LogoImgFilePath = this.txtLogoImgFilePath.Text;

            configuration.CompanyConfigurationKeyValueList.Clear();
            // los key value
            List<CompanyConfigurationKeyValue> configKeyValueList = GetCompanyConfigurationKeyValue(this, BasePage.CompanyId);
            foreach (CompanyConfigurationKeyValue configKeyValue in configKeyValueList)
            {
                TextBox txtBox = (TextBox)pnlMain.FindControl("txt" + configKeyValue.Key);
                if (txtBox != null)
                {
                    CompanyConfigurationKeyValue newKeyValue = new CompanyConfigurationKeyValue();
                    newKeyValue.CompanyId = configuration.CompanyId;
                    newKeyValue.Key = configKeyValue.Key;
                    newKeyValue.Value = txtBox.Text;
                    configuration.CompanyConfigurationKeyValueList.Add(newKeyValue);
                }
            }
        }

        private void Save()
        {
            configuration = WebServiceHelper.SaveCompanyConfiguration(configuration);
            ClearConfigurationCache(this);
        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();
                Bind();
                this.masterPage.ShowMessage(LocalizationProvider.Instance["Bo.Messages.MensajeSaveExitoso"], WebMessageType.Notification);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }       
    }
}